package vashion.azeroth.back.controller.tran;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.common.invitecode.transfer.InviteCodeTransfer;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.personal.pojo.PersonalInviteCode;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalInviteCodeService;

@Controller
@RequestMapping("/back/member")
public class InviteCodeController<T> {

	@Autowired
	private PersonalInviteCodeService<PersonalInviteCode> personalInviteCodeService;
	
	/**
	 * 查询所有邀请码接口,不区分状态与过期，未分页
	 * @param memberId
	 * @return
	 */
	@RequestMapping("getAllInviteCode.htm")
	@ResponseBody
	public ApiResult<List<PersonalInviteCode>> getAllInviteCode(String memberId, String type) {
		if(null == memberId) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "memberId" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		Integer status = InviteCodeTransfer.inviteCodeStatusTransfer(type);
		if(null == status) {
			return ApiResult.newErrorResult(GenericErrorEnum.STATUS_ERROR.getErrorCode(), "type" + GenericErrorEnum.STATUS_ERROR.getErrorMessage());
		}
		
		PersonalInviteCode inviteCodeParam = new PersonalInviteCode();
		inviteCodeParam.setMemberId(Long.valueOf(memberId));
		if(status != 2) {
			inviteCodeParam.setStatus(status);
		}
		
		return personalInviteCodeService.getAllInviteCode(inviteCodeParam);
	}
	
	@RequestMapping("generateInviteCode.htm")
	@ResponseBody
	public ApiResult<PersonalInviteCode> generateInviteCode(String memberId, HttpServletRequest request) {
		if(null == memberId) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "memberId" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		ApiResult<PersonalInviteCode> result = personalInviteCodeService.generateInviteCode(memberId, 0);
		return result;
	}
	
}
